<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="微信名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" clearable filterable placeholder="请选择">
          <el-option :label="'男'" :value="'男'"></el-option>
          <el-option :label="'女'" :value="'女'"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄段" prop="birthday">
        <el-select v-model="queryParams.birthday" clearable filterable placeholder="请选择">
          <el-option v-for="(item, index) in ageArr" :key="index" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="团队" prop="team">
        <el-input v-model="queryParams.team" placeholder="请输入" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否签到" prop="flag">
        <el-select v-model="queryParams.flag" clearable placeholder="请选择">
          <el-option label="已签到" :value="true"></el-option>
          <el-option label="未签到" :value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 页面操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:label:export']">导出</el-button>
        <el-button type="primary" plain icon="el-icon-share" size="mini" :disabled="multiple" @click="handleCouponcode"
          v-hasPermi="['system:label:export']">一键发券</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- table表格 -->
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="微信名称" align="center" prop="userName" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="年龄段" align="center" prop="birthday" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="团队" align="center" prop="team" />
      <el-table-column label="签到" align="center" prop="SignFlag">
        <template slot-scope="scope">
          {{ !scope.row.signFlag ? '暂未签到' : '已签到' }}
        </template>
      </el-table-column>
      <el-table-column label="报名审核" align="center" prop="examine">
        <template slot-scope="scope">
          <el-popover placement="top" trigger="click">
            <div>
              <el-radio-group v-model="scope.row.examine" @change="examineChange(scope.row)">
                <el-radio :label="0" :value="0">未审核</el-radio>
                <el-radio :label="1" :value="1">审核通过</el-radio>
                <el-radio :label="2" :value="2">删除</el-radio>
              </el-radio-group>
            </div>
            <el-button slot="reference">{{ scope.row.examine === 0 ? '未审核' :
                scope.row.examine === 1 ? '审核通过' : scope.row.examine === 2 ? '已删除' : '状态错误'
            }}</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="发券码" align="center" prop="codeTitle">
        <template slot-scope="scope">
          {{ !scope.row.codeTitle ? '暂无券码' : scope.row.codeTitle }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.examine === 1" size="mini" type="text" icon="el-icon-edit"
            @click="handleCouponcode(scope.row)" v-hasPermi="['system:label:edit']">发放券码</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:label:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:label:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改标签对话框 -->
    <!-- <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="真实姓名" prop="realName">
           <el-input v-model="form.realName" placeholder="请输入" />
        </el-form-item>
      </el-form>
       <el-form-item label="性别" prop="sex">
           <el-select v-model="form.sex" clearable filterable placeholder="请选择">
          <el-option :label="'男'" :value="'男'"></el-option>
          <el-option :label="'女'" :value="'女'"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="团队" prop="team">
           <el-input v-model="form.team" placeholder="请输入" />
        </el-form-item>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    <!-- 发送券码对话框 -->
    <!-- 添加或修改标签对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="券码" prop="couponCodeId">
          <el-select v-model="form.couponCodeId" clearable filterable multiple placeholder="请选择">
            <el-option v-for="(item, index) in codelist" :key="index" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUser,
  getUser_export,
  peopleExamine,
  couponCode_list,
  couponCode_sendout,
} from "@/api/view/activity_information";
export default {
  name: "signUp",
  components: {

  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: null,
        phoneNumber: null,
        sex: null,
        userName: null,
        realName: null,
        birthday: null,
        team: null,
        flag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classifyId: [{ required: true, message: "请选择", trigger: "change" }],
        labelName: [{ required: true, message: "请输入", trigger: "blur" }],
        labelImage: [{ required: true, message: "请选择", trigger: "change" }],
      },
      // 标签分类列表
      typeList: [],
      //年龄段数组
      ageArr: [
        "0-20",
        "20-40",
        "40-60",
        "60-80",
        "80-100",
        "100-120",
      ],
      codelist: [], //券码列表
    };
  },
  created() {
    this.getList();
  },
  mounted() {

  },
  methods: {
    /** 查询列表 */
    getList() {
      this.queryParams.activityId = this.$route.query.id;
      this.loading = true;
      getUser(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getCouponCodelist();
    },
    /* 获取券码列表 */
    getCouponCodelist(id) {
      couponCode_list({ activityId: this.queryParams.activityId }).then(res => {
        this.codelist = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        couponCodeId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /* 审核字段操作 */
    examineChange(row) {
      peopleExamine({ activityId: Number(this.queryParams.activityId), examine: row.examine, userId: row.id }).then(res => {
        this.msgSuccess(res.msg);
        this.getList();
      }).catch(err => {
        this.getList();
      });

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签";
    },
    /* 发放券码按钮操作 */
    handleCouponcode(row) {
      this.reset();
      row.couponCodeId = !row.couponCodeId ? [] : row.couponCodeId.split(",").map(Number);
      this.form = !row.id ? this.form : row;
      this.open = true;
      this.title = "发放券码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改标签";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = {
            activityId: Number(this.queryParams.activityId),
            couponCodeId: !JSON.parse(JSON.stringify(this.form)).couponCodeId ? "" : JSON.parse(JSON.stringify(this.form)).couponCodeId.join(","),
            userId: !JSON.parse(JSON.stringify(this.form)).id ? this.ids.join(",") : JSON.parse(JSON.stringify(this.form)).id.toString()
          };
          if (data.userId != null) {
            couponCode_sendout(data).then((response) => {
              this.msgSuccess("发送成功");
              this.open = false;
              this.getList();
            });
          } else {
            // label_add(this.form).then((response) => {
            //   this.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除标签编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return label_delete(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    // /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有报名人信息?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return getUser_export(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    //图片上传事件
    uploadimg(e) {
      this.form.labelImage = e;
    },
    //删除图片事件
    removeImg(e) {
      this.form.labelImage = e;
    },
  },
};
</script>
